-- 来源tips

local fromBtnRender = import(".renders.fromBtnRender")

local newFromBtnView = class("newFromBtnView",function()
    return ccui.Layout:create()
end)

local string_split = string.split
local subBtnHeight = 70 -- 子btn高
local scrollViewWidth = 0 -- 滚动区域宽

function newFromBtnView:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end
    self.mUINode = createModuleNode( Res.Tips_FromBtn, "Panel", self )
    self:setContentSize(self.mUINode:getContentSize())
    ccui.Helper:doLayout( self.mUINode )

    self._shownFlag = false
    self._btn = self.mUINode:getChildByName( "button" )
    self._img_1 = self._btn:getChildByName( "img_1" )
    self._img_1:setVisible( false )
    self._img_2 = self._btn:getChildByName( "img_2" )
    self._img_2:setVisible( true )
    self._listBg = self.mUINode:getChildByName( "bg" )
    self._listBg:setVisible( false )
    self._list = self.mUINode:getChildByName( "ScrollView" )
    self._list:setVisible( false )

    scrollViewWidth = self._list:getContentSize2()
    self._list:removeAllChildren()
    local container = self._list:getInnerContainer()
    self._list.createRender = function (list, idx)
        local items = list:getChildren()
        local render = items[idx]
        if nil == render then
            render = fromBtnRender.create()
            render:setName( "subBtn_" .. idx )
            container:addChild( render )
        end
        render:setVisible(true)
        return render
    end

    display.uiAddClick( self._btn, function()
        if self._shownFlag then -- 之前可见？
            self._img_1:setVisible( false )
            self._img_2:setVisible( true )
            self._listBg:setVisible( false )
            self._list:setVisible( false )
        else
            self._img_1:setVisible( true )
            self._img_2:setVisible( false )
            self._listBg:setVisible( true )
            self._list:setVisible( true )
        end
        self._shownFlag = not self._shownFlag
    end)
end

-- 刷新listView窗口内容
function newFromBtnView:refreshContent( getResource )
    self._list:stopAllActions()
    local container = self._list:getInnerContainer()
    local childNums = self._list:getChildrenCount()

    local datas = string_split( getResource, ";" )
    local len = #datas
    local curIdx = 0
    local function addSubBtn()
        curIdx = curIdx + 1
        if curIdx <= len then
            local render = self._list:createRender(curIdx)
            render:setData(datas[curIdx])
            render:onBtnClick(handler(self, self._onSubBtnClick))
        else
            --删掉多余的item
            self._list:stopAllActions()
            if childNums >= curIdx then
                for i = curIdx, childNums do
                    container:removeChildByName( "subBtn_" .. i )
                end
            end
            self:_refreshListView()
        end
    end

    if len > 0 then
        local items = self._list:getChildren()
        for i,item in ipairs( items ) do
            item:setVisible(false)
        end
        self._list:schedule(addSubBtn, 0)
    end
end

-- 刷新滚动窗口
function newFromBtnView:_refreshListView()
    local container = self._list:getInnerContainer()
    local childNums = self._list:getChildrenCount()

    local height = subBtnHeight * childNums
    container:setContentSize( cc.size( scrollViewWidth, height ) )
    self._list:setContentSize( cc.size( scrollViewWidth, height ) )
    self._listBg:setContentSize( cc.size( scrollViewWidth, height + 14 ) )

    for i,v in ipairs( self._list:getChildren() ) do
        local posY = subBtnHeight * ( childNums - i )
        v:setPositionY( posY )
    end
end

function newFromBtnView:_onSubBtnClick( op, ... )
    executeFunc( self._clickCb, op, ... )
end

function newFromBtnView:setClickCB( cb )
    self._clickCb = cb
end

function newFromBtnView:hide()
    self._shownFlag = false
    self._img_1:setVisible( false )
    self._img_2:setVisible( true )
    self._listBg:setVisible( false )
    self._list:setVisible( false )
end

return newFromBtnView
